######### This script will produce a matrix of species by patchstat indicating significant differences between models within species
#########

#### Load library

library('SDMTools')

#### Directory to read files from

in.dir = '/home1/99/jc152199/skinkpatchstat/'

### Directory to write images to

out.dir = '/home1/99/jc152199/skinkpatchstat/histograms/'

##### Identify species

spp = c('SAPCZEC','SAPTETR','SAPBASI','GNYQUEE','CARRUBR','LAMCOGG','LAMROBE')

#### Read in summary data frame

pss = read.csv(paste(in.dir,'SigTestForAllSkinksPatchStat.csv',sep=''), header=T)

### Loop through each species doing what tapply could probably do in a single fucking call

outdata = NULL

for (s in spp)

	{
	
	### Subset pss to a single species
	
	tpss = pss[which(pss$spp==s),]
	
	#### Create a matrix with
	
	t.out = data.frame(spp=s, n.cell=tpss[1,8], n.core.cell=tpss[2,8], n.edges.perimeter=tpss[3,8], n.edges.internal=tpss[4,8], area=tpss[5,8], core.area=tpss[6,8], perimeter=tpss[7,8], perim.area.ratio=tpss[8,8], shape.index=tpss[9,8], frac.dim.index=tpss[10,8], core.area.index=tpss[11,8])
	
	#### Bind data
	
	outdata = rbind(t.out,outdata)
	
	}
	
### Done

write.csv(outdata, file=paste(in.dir,'SpeciesByStatMatrix.csv',sep=''), row.names=F)
